<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<body>
	<script type="text/javascript">
		$(function() {
			//初始化修改弹窗
			$("#menu_update_dialog").dialog({
				title:"修改菜单",
				width: 300,
			    height: 400,
			    closed: true,
			    cache: false,
			    modal: true,
			    buttons:[{
			    	text:"保存",
			    	handler:function() {
			    		var param = $("#menu_update_form").serialize();
			    		var node = $("#menu_tree").tree("getSelected");
			    		$.ajax({
			    			url:"menu/update.action",
			    			type:"post",
			    			data:param,
			    			dataType:"json",
			    			success:function(data) {
			    				$.messager.alert("提示", data.msg, "info", function() {
			    					if(data.isSuccess) {
										$("#menu_update_dialog").dialog("close");
										$("#menu_tree").tree("reload");
										$("#menu_datagrid").datagrid("reload", {pid:node==null?"":node.id});
			    					}
			    				});
			    			}
			    		});
			    	}
			    },{
			    	text:"关闭",
			    	handler:function() {
			    		$("#menu_update_dialog").dialog("close");
			    	}
			    }]
			});
			
			//初始化新增弹窗
			$("#menu_add_dialog").dialog({
				title:"新增菜单",
				width: 300,
			    height: 400,
			    closed: true,
			    cache: false,
			    modal: true,
			    buttons:[{
			    	text:"保存",
			    	handler:function() {
			    		var param = $("#menu_add_form").serialize();
			    		$.ajax({
			    			url:"menu/add.action",
			    			type:"post",
			    			data:param,
			    			dataType:"json",
			    			success:function(data) {
			    				var node = $("#menu_tree").tree("getSelected");
			    				$.messager.alert("提示", data.msg, "info", function() {
			    					if(data.isSuccess) {
										$("#menu_add_dialog").dialog("close");
										$("#menu_tree").tree("reload");
										$("#menu_datagrid").datagrid("reload", {pid:node.id});
			    					}
			    				});
			    			}
			    		});
			    	}
			    },{
			    	text:"关闭",
			    	handler:function() {
			    		$("#menu_add_dialog").dialog("close");
			    	}
			    }]
			});
			
			$("#menu_datagrid").datagrid({
				width : 'auto',
				height : 'auto',
				fit:true,
				pageSize : 20,
				pageList : [ 5, 10, 20 ],
				nowrap : false,
				striped : true,
				url : "menu/datagrid.action",
				loadMsg : "数据正在加载……",
				pagination : true,
				rownumbers : true,
				columns : [ [ 
				    {field : "ck",checkbox:true} ,      
					{field : 'code',title : '编号',align : 'center',width : '100'},
					{field : 'name',title : '菜单名',align : 'center',width : '100'},
					{field : 'url',title : '链接地址',align : 'center',width : '200'}
				] ],
				toolbar:[{
					text:'新增',
					handler:function() {
						var node = $("#menu_tree").tree("getSelected");
						if(node==null) {
							$.messager.alert("提示", "请在菜单树中选择新增菜单的父节点！！");
						} else {
							var rootNode = getRootNode($("#menu_tree"), node);
							$("#menu_add_dialog").dialog("open").dialog("refresh", "platform/menu/add.jsp?pid="+node.id+"&rid="+rootNode.id);
						}
					}
				},'-',{
					text:'修改',
					handler:function() {
						var nodes = $("#menu_datagrid").datagrid("getChecked");
						if(nodes.length>1) {
							$.messager.alert("提示", "一次只能修改一个菜单！！");
						} else if(nodes.length==0) {
							$.messager.alert("提示", "请选择要修改的菜单");
						} else if(nodes.length==1) {
							$("#menu_update_dialog").dialog("open").dialog("refresh", "menu/loadMenu.action?id="+nodes[0].id);
						} 
					}	 
				},'-',{
					text:'删除',
					handler:function() {
						var nodes = $("#menu_datagrid").datagrid("getChecked");
						$.messager.confirm("确认","你确定要删除这几条记录么？",function(r){
						    if (r){
						        $.ajax({
						        	url:"menu/del.action",
						        	type:"post",
						        	data:{ids:getNodeIds(nodes)},
						        	dataType:"json",
						        	success:function(data) {
						        		var node = $("#menu_tree").tree("getSelected");
						        		$.messager.alert("提示", data.msg, "info", function() {
						        			if(data.isSuccess) {
						        				$("#menu_tree").tree("reload");
												$("#menu_datagrid").datagrid("reload", {pid:node.id});
						        			}
						        		});
						        	}
						        });
						    }
						});
					}
				}]
			});
		});
		
		function getRootNode(tree, node) {
			var pnode = tree.tree("getParent", node.target);
			var rnode = null;
			if(pnode!=null) {
				rnode = getRootNode(tree, pnode);
			} else {
				return node;
			}
			return rnode;
		}
		
		function getNodeIds(nodes) {
			var ids = "";
			$.each(nodes, function(i, n){
				ids += n.id
				if(i<nodes.length-1) ids += ",";
			});
			return ids;
		}
	</script>
	<table id="menu_datagrid"></table>
	<div id="menu_add_dialog"></div>
	<div id="menu_update_dialog"></div>
</body>
